const app = getApp(),url = app.globalData.url;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    xl_header:{'id':'0','name':'选择学历'},
    xl:[
      {'id':8,'name':'初中及以下'},
      {'id':7,'name':'高中'},
      {'id':6,'name':'中技'},
      {'id':5,'name':'中专'},
      {'id':4,'name':'大专'},
      {'id':3,'name':'本科'},
      {'id':2,'name':'硕士'},
      {'id':1,'name':'博士'}
    ],//学历
    xlindex:'',
    id:0,//学历id

    dateValue_s:'开始时间',
    dateValue_e:'至今',
    dateValue_s_v:'',
    dateValue_d_v:'',
    education:'',//学历
    startTime:'',
    endTime:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that = this;
    var date = that.formatTime(new Date);
    that.setData({
      dateValue_d_v:date,
      id:options.id
    });
    if(options.id > 0){
      
      var xl = that.data.xl
      wx.request({
        url: url + 'cvEduction/getCVEducationById',//学历列表 
        method: "GET",
        data:{id:options.id },
        success:function(res){
          console.log("教育经历信息详情",res);
          var ret = res.data
          if(ret.code == 200){
            that.setData({
              xldetail:ret.data,
              college:ret.data.college,	//学校
              education:ret.data.education,	//学历/学位，1：博士，2：硕士，3：本科，4：大专，5：中专，6：中技，7：高中，8：初中以下
              endTime:ret.data.endTime,	//结束时间
              special:ret.data.special,	//专业
              specialDescribe:ret.data.specialDescribe,	//专业描述
              startTime:ret.data.startTime,	//开始时间
            })
            // var xllevel = ret.data.education
            // for(let item in xl){
            //   if(xl[item].id == xllevel){
            //     that.setData({xlindex:item,})
            //     break;
            //   }
            // }
          }else{
            that.setData({xldetail:''})
            wx.showToast({
              title: ret.msg,
              icon:'none'
            })
          }
        }
      })
    }else{
      console.log('=0')
      return false
    }
  },
  //学校
  school(e){
    var that = this
    that.setData({college:e.detail.value})
  },
  //学历
  xinziChange: function (e) {
    var that = this;
    var val = e.detail.value//这个是  index
    var xl = that.data.xl
    for(let item in xl){
      if(item == val){
        that.setData({education:xl[item].name,})
        // that.setData({education:xl[item].id,})
        break;
      }
    }
    that.setData({xlindex:val,})//页面赋值 title:name
  },
  //专业
  zhuanye(e){
    var that = this
    that.setData({special:e.detail.value})
  },
  // startTime(e){
  //   var that = this
  //   that.setData({startTime:e.detail.value})
  // },
  // endTime(e){
  //   var that = this
  //   that.setData({endTime:e.detail.value})
  // },
  bindDateChange: function(e) {
    console.log('startTime', e.detail.value)
    this.setData({
      startTime: e.detail.value
    })
  },
  jieshuChange: function(e) {
    console.log('endTime', e.detail.value)
    this.setData({
      endTime: e.detail.value
    })
  },


  des(e){
    var that = this
    that.setData({specialDescribe:e.detail.value})
  },
  del(){
    var that = this
    var id = that.data.id
    wx.request({
      url: url + 'cvEduction/delete?id='+id,
      method: "DELETE",
      // data:{id:id },
      success:function(res){
        console.log("删除经历",res);
        var ret = res.data
        if(ret.code == 200){
          wx.showToast({
            title:'删除成功',
            icon:'none',
          })
          setTimeout(function(){
            wx.navigateBack({
              delta: 1,
            })
          },1000)
          
        }else{
          
          that.setData({sub:true})
          wx.showToast({
            title: '删除失败',
            icon:'none'
          })
        }
      }
    })
  },
  datePickerBindchange:function(e){
    this.setData({
      dateValue_s:e.detail.value,
      dateValue_s_v:e.detail.value
    })
  },
  datePickerBindchangeend:function(e){
    this.setData({
      dateValue_e:e.detail.value,
      dateValue_e_v:e.detail.value
    })
  },
  formatTime:function(date){
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    if(month < 10){
      month = '0'+month;
    }
    if(day < 10){
      day = '0'+day;
    }
    return year+'-'+month+'-'+day;
  },

  checkSensWords(e){
    let itemData = e.target.dataset;
    let label = Object.values(itemData)[0]
    let key = Object.keys(itemData)[0]
    this.sensWords = this.sensWords || {}
    app.validSensWords(e.detail.value, label).then(() => {
      delete this.sensWords[key]
    }).catch(() => {
      this.sensWords = {...itemData, ...this.sensWords}
    })
  },
  savedata(e){
    var that = this
    var xlid = that.data.id
    if(xlid > 0){
      //编辑
      var method = 'PUT'
      var api = 'cvEduction/updateEducation'
      var xlid = xlid
    }else{
      // 添加
      var method = 'POST'
      var api = 'cvEduction/add'
      var xlid = 0
    }
    var uid = wx.getStorageSync('user_massage')
    if(that.data.college == '' || that.data.college == undefined){
      wx.showToast({
        title: '请填写学校名称',
        icon:'none',
      })
      return false
    }
    if(that.data.education == '' || that.data.education == undefined){
      wx.showToast({
        title: '请填写学历',
        icon:'none',
      })
      return false
    }
    if(that.data.startTime == '' || that.data.endTime == ''){
      wx.showToast({
        title: '请填写时间',
        icon:'none',
      })
      return false
    }
    if(that.data.special == '' || that.data.special == undefined){
      wx.showToast({
        title: '请填写专业',
        icon:'none',
      })
      return false
    }
    if(that.data.specialDescribe == '' || that.data.specialDescribe == undefined){
      wx.showToast({
        title: '请填写专业描述',
        icon:'none',
      })
      return false
    }
    let sensWords = Object.values(this.sensWords || {})
    if(sensWords.length > 0){
      wx.showToast({
        title: `${sensWords.join(',')}包含敏感词，请修改后再保存`,
        icon:'none',
      })
			return false;
    }
    var data = {
      approval: '1',
      college:that.data.college,	//学校
      createTime:'',	//创建时间
      education:that.data.education,	//学历/学位，1：博士，2：硕士，3：本科，4：大专，5：中专，6：中技，7：高中，8：初中以下
      endTime:that.data.endTime,	//结束时间
      id:xlid,//教育经历ID
      special:that.data.special,	//专业
      specialDescribe:that.data.specialDescribe,	//专业描述
      startTime:that.data.startTime,	//开始时间
      updateTime:'',	//更新时间
      userUid:uid.userUid,	//用户编号
    }
    wx.request({
      url:url + api,
      header:{
      'context-type': 'application/json',
      },
      method: method,
      data:data,
      success:function(res){
        var ret = res.data
        // console.log(res,'412342123421')
        if(ret.code == 200){
          wx.showToast({
            title: '保存成功',
            icon:'none',
          })
          setTimeout(function(){
            wx.redirectTo({
              url:'/HomePage/linejianli/linejianli'
            })
          },1000)
        }else{
          wx.showToast({
            title:'保存失败',
            icon:'none',
          })
        }
      }
    })
  
  //  wx.navigateTo({
  //        url:'/HomePage/linejianli/linejianli'
  //   })
  }
})